package com.putao.album.photodetail;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.putao.album.R;
import com.putao.album.base.BaseFragment;
import com.putao.album.photodetail.bean.PhotoViewItem;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.widget.rounder.RoundProgressBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by chen on 15-6-5.
 */
public class PhotoViewFragment extends BaseFragment {
    private ImageView imageView;
    private int index;
    private PhotoViewItem item;
    private RoundProgressBar round_progress_bar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_view, container, false);
        imageView = (ImageView) view.findViewById(R.id.photo);
        round_progress_bar = (RoundProgressBar)view.findViewById(R.id.round_progress_bar);

        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt("index");
            String photoId = bundle.getString("photoId");
            getPhotoItemDetail(photoId);
        }

        return view;
    }

    private void getPhotoItemDetail(final String photo_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("photo_id", photo_id);
        Loger.d("chen+++getPhotoItemDetail()++photo_id=" + photo_id);
        HttpRequestUtil.doHttpGetRequest(mActivity, PuTaoConstants.API_GET_PHOTO_DETAIL,
                new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), true,
                new HttpRequestUtil.RequestResponseListener() {
                    @Override
                    public void success(int statuscode, String response) {

                        try {
                            JSONObject json = new JSONObject(response);
                            String status = json.getString("status");
                            String data = json.getString("data");
                            if (status.equals(HttpRequestUtil.ResponseCode.api_code_200)) {

                                item = new Gson().fromJson(data, PhotoViewItem.class);

                                final DisplayImageOptions options = new DisplayImageOptions.Builder()
                                        //.showImageOnLoading(BitmapHelper.getLoadingDrawable()).showImageOnFail(BitmapHelper.getLoadingDrawable()).showImageForEmptyUri(BitmapHelper.getLoadingDrawable())
                                        .considerExifParams(true).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();

                                ImageLoader.getInstance().displayImage(item.getPhoto(), imageView, options, new ImageLoadingListener() {
                                    @Override
                                    public void onLoadingStarted(String imageUri, View view) {

                                    }

                                    @Override
                                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                                        round_progress_bar.setVisibility(View.INVISIBLE);
                                    }

                                    @Override
                                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                        round_progress_bar.setVisibility(View.INVISIBLE);
                                    }

                                    @Override
                                    public void onLoadingCancelled(String imageUri, View view) {
                                        round_progress_bar.setVisibility(View.INVISIBLE);

                                    }
                                }, new ImageLoadingProgressListener() {
                                    @Override
                                    public void onProgressUpdate(String imageUri, View view, int current, int total) {
                                        int value = current * 100 / total;

                                        round_progress_bar.setProgress(value);
                                    }
                                });

                                ActivityPhotoDetail mActivity = ((ActivityPhotoDetail) getActivity());
                                if (mActivity != null) {
                                    mActivity.upCurDateViewUi(index, item);
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void failed(int statuscode, String response) {
                    }
                });
    }

    public PhotoViewItem getItem() {
        return item;
    }
}
